/**
 * @Auther: ChenforCode
 * @Date: 2018/9/12 08:31
 * @Description:
 */
package cn.chenforcode.service;

import cn.chenforcode.dao.AdminStuRecordDAO;
import cn.chenforcode.dao.StuAdminDAO;
import cn.chenforcode.dao.StudentDAO;
import cn.chenforcode.entity.AdminStuRecord;
import cn.chenforcode.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentDAO studentDAO;

    @Autowired
    private AdminStuRecordDAO adminStuRecordDAO;

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成增加学生的逻辑 管理员用
     * @Date 9:18 2018/9/12
     * @Param [student, aname]
     **/
    public String addStudent(Student student, String aname) {
        if (studentDAO.queryCanAdd(student.getSno()) == 1) {
            //加学生
            studentDAO.addStudent(student);
            //加记录
            AdminStuRecord adminStuRecord = new AdminStuRecord(aname, student.getSno(), new Timestamp(new Date().getTime()));
            adminStuRecordDAO.addAdminStuRecord(adminStuRecord);
            return "success";
        }
        return "failed";
    }

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成增加学生的逻辑 注册使用
     * @Date 11:21 2018/9/14
     * @Param [student]
     **/
    public String addStudent(Student student) {
        if (studentDAO.queryCanAdd(student.getSno()) == 1) {
            //加学生
            studentDAO.addStudent(student);
            return "success";
        }
        return "failed";
    }

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成删除学生的逻辑
     * @Date 9:36 2018/9/12
     * @Param [sno, aname]
     **/
    public String deleteStudent(String sno, String aname) {
        if (studentDAO.queryCanDelete(sno) == 1) {

            //删除学生
            studentDAO.deleteStudentBySno(sno);
            //加记录
            AdminStuRecord adminStuRecord = new AdminStuRecord(aname, sno, new Timestamp(new Date().getTime()));
            adminStuRecordDAO.addAdminStuRecord(adminStuRecord);
            return "success";
        }
        return "failed";
    }

    /**
     * @return java.lang.String
     * @Author ChenforCode
     * @Description //TODO 完成更新学生的逻辑
     * @Date 9:41 2018/9/12
     * @Param [student, aname]
     **/
    public String updateStudet(Student student, String aname) {
        //更新
        studentDAO.updateStudent(student);
        //加记录
        AdminStuRecord adminStuRecord = new AdminStuRecord(aname, student.getSno(), new Timestamp(new Date().getTime()));
        adminStuRecordDAO.addAdminStuRecord(adminStuRecord);
        return "success";
    }

    /**
     * @return cn.chenforcode.entity.Student
     * @Author ChenforCode
     * @Description //TODO 完成查询学生的逻辑(管理员使用)
     * @Date 9:41 2018/9/12
     * @Param [sno, aname]
     **/
    public Student getStudent(String sno, String aname) {
        //查询
        if (studentDAO.getStudentBySno(sno) != null) {
            //加记录
            AdminStuRecord adminStuRecord = new AdminStuRecord(aname, sno, new Timestamp(new Date().getTime()));
            adminStuRecordDAO.addAdminStuRecord(adminStuRecord);
            return studentDAO.getStudentBySno(sno);
        }
        return null;
    }

    /**
     * @return cn.chenforcode.entity.Student
     * @Author ChenforCode
     * @Description //TODO 完成查询学生的逻辑(普通使用)
     * @Date 8:19 2018/9/14
     * @Param [sno]
     **/
    public Student getStudent(String sno) {
        return studentDAO.getStudentBySno(sno);
    }

    /**
     * @return java.util.List<cn.chenforcode.entity.Student>
     * @Author ChenforCode
     * @Description //TODO 完成得到所有学生的逻辑
     * @Date 9:43 2018/9/12
     * @Param [aname]
     **/
    public List<Student> getAllStudents(String aname) {
        //加记录
        AdminStuRecord adminStuRecord = new AdminStuRecord(aname, "all", new Timestamp(new Date().getTime()));
        adminStuRecordDAO.addAdminStuRecord(adminStuRecord);
        return studentDAO.getAllStudents();
    }

    /**
     * @return java.util.List<cn.chenforcode.entity.Student>
     * @Author ChenforCode
     * @Description //TODO 完成得到所有学生的逻辑（普通使用）
     * @Date 8:20 2018/9/14
     * @Param []
     **/
    public List<Student> getAllStudents() {
        return studentDAO.getAllStudents();
    }

    /**
     * @return void
     * @Author ChenforCode
     * @Description //TODO 完成充值的业务逻辑
     * @Date 8:35 2018/9/14
     * @Param [sno, add]
     **/
    public void addBalance(String sno, long add) {
        studentDAO.addBanance(sno, add);
    }
}
